package gates;

import register.Register;
import gates.FunctionGate;

/**
 * This is the Marking subroutine M of the grover iteration. Also referred to as oracle. 
 * if x=t is searched state, and x != t any other state let:
 * f(x !=t)=0 and f(x=t) = 1
 * M: |x> --> (-1)^f(x)|x> 
 * 
 * @author Jan Zaucha
 *
 */
public class OracleFunction extends FunctionGate {

	/** Searched element the algorithm is looking to find */
	private int searched;

	/**
	 * Constructor 
	 * @param searched the searched element
	 */
	protected OracleFunction(int searched){
		this.searched = searched;
	}


	/**
	 * Returns the name of the gate regardless of representation.
	 */
	public String getName() {
		return "Oracle";
	}

	/**
	 * Flips just the searched state
	 *
	 * @param r the register 
	 */
	public void apply(Register r) {
		if (searched < 0 || searched >= r.getNumStates()){
			throw new IllegalArgumentException("Searched state is not in the register. " +
					"Searched state: |" +searched + ">  Number of states in register: " + r.getNumStates() );
		}
		r.getElement(0, searched).negate();

	}



}
